let KoaRouter = require('koa-router');
let jwt = require('jsonwebtoken');
let Fly = require("flyio/src/node")
let fly = new Fly;

const router = new KoaRouter();

// home主页数据title+floor
let indexData = require('../datas/index.json');
router.get('/getIndexData', (ctx) => {
	ctx.body = indexData;
});
//home-warterfull
let indexwarterFull = require('../datas/indexwarterFull.json');
router.get('/indexwarterFull', (ctx) => {
	ctx.body = indexwarterFull;
});
//分类页数据
let categoryDatas = require('../datas/categoryDatas.json');
router.get('/getCateGoryData', (ctx) => {
	ctx.body = categoryDatas;
});

// 接受用户发送过来的登录凭证
router.get('/getOpenId',async (ctx) => {
  let code = ctx.query.code
  let appId = 'wx96c71d20c0194b21'
  let secret = '43adde62aae8d436de9f1e9811c472c2'
  let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
  
  const result = await fly.get(url)
  let openid = JSON.parse(result.data).openid 
  let token = jwt.sign({
    data: openid
  }, 'zhaoliying', { expiresIn: '1day' });
        
  ctx.body = token
});


router.get('/verifyToken',ctx => {
  let token = ctx.query.token
  let result = jwt.verify(token, 'zhaoliying');
  console.log(result)

})
module.exports = router;
